package com.lanou.weekend02.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.weekend02.bean.AjaxResult;
import com.lanou.weekend02.bean.Student;
import com.lanou.weekend02.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lizhongren1.
 */
@Controller
@CrossOrigin
public class MainController {

    @Resource
    private StudentService studentService;

    // 正常的分层
    // controller层, 和前端进行交互,负责处理请求映射
    // service层, 主要处理业务逻辑的地方,处理第三方的数据
    // dao层(数据层), 访问数据库

    @ResponseBody
    @RequestMapping("/json")
    public Student testJson(){
        return studentService.findStudent();
    }

    @ResponseBody
    @RequestMapping("/all")
    public List<Student> allStu(){
        return studentService.findAllStudent();
    }

    @ResponseBody
    @RequestMapping("/stu")
    public Student findStuById(
            @RequestParam("sid") Integer id){

        return studentService.searchStuById(id);
    }

    // 从前端接收很多数据
    // 比如注册新用户
    @ResponseBody
    @RequestMapping("/testall")
    public AjaxResult testAll(){

        List<Student> allStudent =
                studentService.findAllStudent();

        AjaxResult ar = new AjaxResult();

        ar.setMsg("查询成功!");
        ar.setCode(1000);
        ar.setResult(allStudent);

        return ar;
    }

    @ResponseBody
    @RequestMapping(value = "/addNewStu", method = RequestMethod.POST)
    public AjaxResult insertStudent(HttpServletRequest request, Student stu){

        String ip = request.getHeader("X-FORWARDED-FOR");

        if (ip == null || "".equals(ip)){
            System.out.println(request.getRemoteAddr());
        }

        System.out.println(stu);
        boolean b = studentService.insertNewStudent(stu);

        AjaxResult ar = new AjaxResult();

        if (b){
            ar.setMsg("添加成功!");
            ar.setCode(1000);
            ar.setResult(stu);
        } else {
            ar.setMsg("添加失败!");
            ar.setCode(1001);
        }
        return ar;
    }

//    @ResponseBody
//    @RequestMapping("/pageinfo")
//    public PageInfo pageInfo(){
//
//    }

    @ResponseBody
    @RequestMapping("/allstu")
    public PageInfo stuByPage(HttpServletRequest request,
                              @RequestParam("page") int page,
                              @RequestParam("size") int size){

        String ip = request.getHeader("X-FORWARDED-FOR");

        if (ip == null || "".equals(ip)){
            System.out.println(request.getRemoteAddr()+":page="+page+",size="+size);
        }

        PageHelper.startPage(page,size);

        List<Student> allStudent = studentService.findAllStudent();
        PageInfo<Student> studentPageInfo = new PageInfo<>(allStudent, 5);

        return studentPageInfo;
    }




}
